<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-form-pane .layui-input-block{
            border: 1px solid #eee;
        }
        .layui-tab-card{
            height: 100%;
        }
        .pane_info{
            height: 100%;
            box-sizing:border-box;
        }
        .layui-tab-item{
            padding: 10px;
        }
        .layui-table .layui-form-item{
            margin-bottom: 0px;
        }
        .role_list_{
            margin-bottom: 10px;
        }
        .tr_{
            background-color: #FBFBFB;
            text-align: center;
            width: 230px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px" class="search_pane">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="realname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline" pane="">
                            <label class="layui-form-label">用户性别</label>
                            <div class="layui-input-block">
                                <input type="radio"  name="sex" value="0" title="男" >
                                <input type="radio"  name="sex" value="1" title="女" >
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户手机</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phone" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户邮箱</label>
                            <div class="layui-input-inline">
                                <input type="text" name="email" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<!--新增-->
<script type="text/html" id="data_operation">
    <div class="pane_info">
        {{#  if(!d.edit){ }}
        <div class="disabled_pane"></div>
        {{#  } }}
        <form class="layui-form layui-form-pane" action="" lay-filter="demo-lay-filter-datainfo">
            {{#  if(d.userId){ }}
            <input type="hidden"  name="userId" value="{{d.userId}}" >
            {{#  } }}
            <div class="layui-form-item">
                <label class="layui-form-label">用户账号</label>
                <div class="layui-input-block">
                    <input type="text" name="username" {{ (d.type == 'update') ? 'disabled' : '' }} required  lay-verify="required" value="{{d.username}}" autocomplete="off" placeholder="请输入用户账号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="realname" required  lay-verify="required" value="{{d.realname}}" autocomplete="off" placeholder="请输入用户姓名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" pane="">
                <label class="layui-form-label">用户性别</label>
                <div class="layui-input-block">
                    <input type="radio"  name="sex" value="0" title="男" {{ d.sex==0 ? 'checked' : '' }}  >
                    <input type="radio"  name="sex" value="1" title="女" {{ d.sex==1 ? 'checked' : '' }}>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户地址</label>
                <div class="layui-input-block">
                    <input type="text" name="address" required  lay-verify="required" value="{{d.address}}" autocomplete="off" placeholder="请输入用户住址" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户手机</label>
                <div class="layui-input-block">
                    <input type="text" name="phone" required  lay-verify="required" value="{{d.phone}}" autocomplete="off" placeholder="请输入用户手机号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email" required  lay-verify="required" value="{{d.email}}" autocomplete="off" placeholder="请输入邮件地址" class="layui-input">
                </div>
            </div>
        </form>
    </div>
</script>

<!--修改-->
<script type="text/html" id="data_operation_update">
    <div class="pane_info">
        <div class="layui-tab layui-tab-card">
            <ul class="layui-tab-title">
                <li class="layui-this">用户信息</li>
                <li>授权角色</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <form class="layui-form layui-form-pane" action="" >
                        {{#  if(d.userId){ }}
                        <input type="hidden"  name="userId" value="{{d.userId}}" >
                        {{#  } }}
                        <table class="layui-table">
                            <tbody>
                            <tr>
                                <td>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">用户账号</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="username" {{ (d.type == 'update') ? 'disabled' : '' }} required  lay-verify="required" value="{{d.username}}" autocomplete="off" placeholder="请输入用户账号" class="layui-input">
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">用户姓名</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="realname" required  lay-verify="required" value="{{d.realname}}" autocomplete="off" placeholder="请输入用户姓名" class="layui-input">
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="layui-form-item" pane="">
                                        <label class="layui-form-label">用户性别</label>
                                        <div class="layui-input-block">
                                            <input type="radio"  name="sex" value="0" title="男" {{ d.sex==0 ? 'checked' : '' }}  >
                                            <input type="radio"  name="sex" value="1" title="女" {{ d.sex==1 ? 'checked' : '' }}>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">用户地址</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="address" required  lay-verify="required" value="{{d.address}}" autocomplete="off" placeholder="请输入用户住址" class="layui-input">
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">用户手机</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="phone" required  lay-verify="required" value="{{d.phone}}" autocomplete="off" placeholder="请输入用户手机号" class="layui-input">
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">用户邮箱</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="email" required  lay-verify="required" value="{{d.email}}" autocomplete="off" placeholder="请输入邮件地址" class="layui-input">
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">身份证号</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="idcard" id="idcard" required  lay-verify="required" value="{{d.idcard}}" autocomplete="off" placeholder="请输入用户身份证号码" class="layui-input">
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">用户生日</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="birthday" id="birthday"  lay-verify="required" value="{{d.birthday}}" placeholder="请输入用户生日" class="layui-input">
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">用户介绍</label>
                                        <div class="layui-input-block">
                                            <textarea placeholder="请输入内容" name="descnote" value="{{d.descnote}}" class="layui-textarea"></textarea>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                        <div class="button_group">
                            <button class="layui-btn layui-btn-primary" lay-submit lay-filter="data-user-add-btn" >提交</button>
                        </div>
                    </form>
                </div>
                <div class="layui-tab-item">
                    <div class="role_area">
                        <table class="layui-table info-pane">
                            <tbody>
                            <tr>
                                <td class="tr_">账号</td>
                                <td>{{d.username}}</td>
                            </tr>
                            <tr>
                                <td class="tr_">用户名</td>
                                <td>{{d.realname}}</td>
                            </tr>
                            </tbody>
                        </table>
                        <hr>
                        <div>
                            <form class="layui-form layui-form-pane"  lay-filter="demo-lay-filter-rolesearch" action="">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">角色名称</label>
                                        <div class="layui-input-inline" style="width: 350px">
                                            <input type="text" name="name" autocomplete="off" class="layui-input" width="300px">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-role-search-btn"><i class="layui-icon"></i> 搜 索</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div>
                            <fieldset class="layui-elem-field">
                                <legend>角色列表</legend>
                                <div class="role_list_">
                                    <form class="layui-form layui-form-pane" lay-filter="demo-lay-filter-rolechecked" action="">
                                        <div id="role_base_node">
                                        </div>
                                    </form>
                                </div>
                            </fieldset>
                            <div>
                                <button type="button" class="layui-btn layui-btn-normal" lay-on="submitUpdateRole">保存修改</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../js/jquery-3.7.1.min.js"></script>
<script src="../js/crypto-js.min.js"></script>
<script src="../js/common.js"></script>
<script src="/lay/t-secrypt-script.js"></script>
<script>
    layui.use(['form', 'table','laytpl','laydate','util'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var laydate = layui.laydate;
        var util = layui.util;
        var laytpl = layui.laytpl; //模版

        table.render({
            elem: '#currentTableId',
            url: '/sys_user/page',
            method:'POST',
            contentType:'application/json',
            toolbar: '#toolbarDemo',
            request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'userId', width: 80, title: 'ID', sort: true},
                {field: 'username', width: 80, title: '用户账号'},
                {
                    field: 'sex', width: 80, align: 'center', templet: function (d) {
                        if (d.sex == 0) {
                            return '<span class="layui-badge layui-bg-blue">男</span>';
                        }
                        if (d.sex == 1) {
                            return '<span class="layui-badge layui-bg-gray">女</span>';
                        } else {
                            return '<span class="layui-badge-rim">未知</span>';
                        }
                    }, title: '性别'
                },
                {field: 'realname', width: 150, title: '姓名'},
                {field: 'address', width: 180, title: '住址'},
                {field: 'phone', title: '手机号码', minWidth: 150},
                {field: 'email', width: 180, title: '邮箱', sort: true},
                {field: 'createTime', width: 180, title: '创建时间', sort: true},
                {title: '操作',fixed: 'right', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    search: data.field
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                let initData={ username:"",sex:0,phone:"",address:"",email:"",realname:"" };
                add(initData);
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if(data.length==0){
                    return layer.msg('未选择数据', {icon: 0});
                }
                let arr=[];
                for (let i = 0; i < data.length ; i++) {
                    let o=data[i];
                    arr.push(o.userId+'');
                }
                layer.confirm('真的删除选中账号吗', function (index) {
                    layer.close(index);
                    del(arr);
                })
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                update(data);
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除'+data.username+'账号吗', function (index) {
                    layer.close(index);
                    let arr=[];
                    arr.push(data.userId+'')
                    del(arr);
                });
            }
        });

        function del(ids){
            let idData={ids:ids};
            layutils.ajaxJsonPost('/sys_user/delete',JSON.stringify(idData),function (res){
                if(res.code==200){
                    layer.msg(res.message, {icon: 1});
                    table.reload('currentTableId');
                }else {
                    layer.msg(res.message, {icon: 2});
                }
            });
        }

        function add(data){
            data.edit=true;
            data.type='add'
            templateRender('data_operation',data,function (html){
                layerPage('添加操作',html,data,'50%','70%',function (index){
                    let json = form.val('demo-lay-filter-datainfo');
                    layutils.ajaxJsonPost('/sys_user/saveorupdate',JSON.stringify(json),function (res){
                        if(res.code==200){
                            layer.msg(res.message, {icon: 1},function (){
                                layer.close(index);
                            });
                            table.reload('currentTableId');
                        }else {
                            layer.msg(res.message, {icon: 2});
                        }
                    })
                })
                form.render();
            })
        }

        function update(data){
            data.edit=true;
            data.type='update'
            layutils.objNullSetDefalutValue(data,'');//添加默认值
            templateRender('data_operation_update',data,function (html){
                layerFullPageNoButton('编辑用户',html,data);
            })
            form.render();
            laydate.render({
                elem: '#birthday' //指定元素
            });
            let uid=data.userId;
            util.event('lay-on', {
                submitUserInfoUpdate: function(othis){
                    let json = form.val('demo-lay-filter-update');

                },
                //保存用户更新角色
                submitUpdateRole:function (othis){
                    let json = form.val('demo-lay-filter-rolechecked');
                    layer.msg(JSON.stringify(json));
                    let roleNos=[];
                    for (const key in json) {
                        if (json[key] === 'on') {
                            roleNos.push(key)
                        }
                    }
                    let data={userId:uid,roleNos:roleNos};
                    layutils.ajaxJsonPost('/userrole/save',JSON.stringify(data),function (e){
                        if(e.code==200){
                            layer.msg(e.message, {icon: 1});
                        }else {
                            layer.msg(e.msg, {icon: 2});
                        }
                    })
                }
            });
            form.on('submit(data-role-search-btn)', function (e) {
                let d=e.field;
                renderRoleForm(uid,d.name);
                return false;
            })
            form.on('submit(data-user-add-btn)',function (e){
                let json=e.field;
                layutils.ajaxJsonPost('/sys_user/saveorupdate',JSON.stringify(json),function (res){
                    if(res.code==200){
                        layer.msg(res.message, {icon: 1},function (){
                            layer.close(index);
                        });
                        table.reload('currentTableId');
                    }else {
                        layer.msg(res.message, {icon: 2});
                    }
                })
                return false;
            })
            renderRoleForm(uid,'');
            $('#idcard').on('change', function() {
                // 用户完成输入并改变焦点后执行的逻辑
                const inputValue = $(this).val();
                const reg18 = /^\d{17}[\dX]$/i; // 18位身份证号码正则
                if(reg18.test(inputValue)){
                    const birth = inputValue.substring(6, 14); // 获取出生日期部分：yyyyMMdd
                    const formattedDate = `${birth.substring(0, 4)}-${birth.substring(4, 6)}-${birth.substring(6, 8)}`;
                    $("#birthday").val(formattedDate);
                }
            });
            // templateRender('data_operation',data,function (html){
            //     layerFullPage('修改操作',html,data,function (index){
            //         let json = form.val('demo-lay-filter-datainfo');
            //         lay.ajaxJsonPost('/sys_user/saveorupdate',JSON.stringify(json),function (res){
            //             if(res.code==200){
            //                 layer.msg(res.message, {icon: 1},function (){
            //                     layer.close(index);
            //                 });
            //                 table.reload('currentTableId');
            //             }else {
            //                 layer.msg(res.message, {icon: 2});
            //             }
            //         })
            //     })
            //     form.render();
            // })
        }

        //动态渲染表单
        function renderRoleForm(uid,name){
            let id='role_base_node';
            let str='';
            layutils.ajaxGet('/role/getrolelist?name='+name,null,function (e){
                if(e.code==200){
                    let arr=e.data;
                    let str='';
                    for (let i = 0; i < arr.length ; i++) {
                        let obj=arr[i];
                        str+='<input type="checkbox" name="'+obj.roleno+'" lay-skin="primary" title="'+obj.roleName+'">'
                    }
                    $("#role_base_node").empty();
                    $("#role_base_node").html(str);
                    form.render();
                }
            })
            layutils.ajaxGet('/userrole/getbyuid?uid='+uid,null,function (e){
                if(e.code==200){
                    let list=e.data
                    let obj={};
                    if(list.length>0){
                        for (let i = 0; i < list.length; i++) {
                            let o=list[i];
                            let roleno=o.roleNo;
                            obj[roleno]='on';
                        }
                    }
                    form.val('demo-lay-filter-rolechecked',obj);
                }
            })
        }
        //模板渲染
        function templateRender(templateId,data,callback){
            var getTpl = $('#'+templateId).html();
            laytpl(getTpl).render(data, function(html){
                callback(html);
            });
        }
        //操作弹窗，充满整页
        function layerFullPage(title,html,data,callback){
            let index=layer.open({
                type: 1
                ,title: title
                ,shade: 0.2,
                 maxmin:true,
                 shadeClose: true,
                 area: ['100%', '100%']
                ,content: html
                ,btn: ['确认', '取消']
                ,yes: function(){
                    callback(index);
                }
                ,btn2: function(){
                    layer.close(index);
                }
            });
        }
        //操作弹窗，充满整页
        function layerFullPageNoButton(title,html,data){
            let index=layer.open({
                type: 1
                ,title: title
                ,shade: 0.2,
                shadeClose: true,
                area: ['100%', '100%']
                ,content: html
            });
        }
        //操作弹窗
        function layerPage(title,html,data,width,height,callback){
            let index=layer.open({
                type: 1
                ,title: title
                ,area: [width, height]
                ,content: html
                ,btn: ['确认', '取消']
                ,yes: function(){
                    callback(index);
                }
                ,btn2: function(){
                    layer.close(index);
                }
            });
        }


    });
</script>

</body>
</html>